package com.ymatou.iapi.productlimit.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ymatou.iapi.productlimit.parameter.CatalogLimit;
import com.ymatou.iapi.productlimit.parameter.GetLimitAllowBuyBean;
import com.ymatou.iapi.productlimit.parameter.OperationProductLimit;
import com.ymatou.iapi.productlimit.parameter.OperationProductLimitBean;
import com.ymatou.iapi.productlimit.service.GetLimitAllowBuyCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.ProductLimitCallService;
import com.ymttest.utils.TestDataManager;

/**
 * 获取限购信息 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetLimitAllowBuy {
	private static GetLimitAllowBuyBean getlimitallowbuyBean;
	private static GetLimitAllowBuyCall getlimitallowbuyCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取限购信息");
	}

	@Before
	public void caseUp() {
		getlimitallowbuyBean = new GetLimitAllowBuyBean();
		getlimitallowbuyCall = new GetLimitAllowBuyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getlimitallowbuyCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyData(GetLimitAllowBuyCall getlimitallowbuyCall2, OperationProductLimitBean operationproductlimitBean) {
		// Thread:main 2017-08-11 14:08:02.093
		// 返回报文{"Code":200,"BCode":0,"Msg":"操作成功","Data":{"Results":[{"ProductId":"2467b5d7-47ea-4e38-af2e-5602073c4d4d","LimitNumber":0,"ActivityLimitNumber":1,"BoughtNumber":0,"ActivityBoughtNumber":1,"ProductInActivityLimitNumber":0,"ProductInActivityBoughtNumber":0}]},"Trace":null}
		Map<String, OperationProductLimit> mop = new HashMap<String, OperationProductLimit>();
		// 用于和查询比较
		for (OperationProductLimit topl : operationproductlimitBean.getOperationProductLimitList()) {
			mop.put(topl.getProductId(), topl);
		}
		JsonArray results = getlimitallowbuyCall2.getJsonObject().getAsJsonObject("Data").getAsJsonArray("Results");
		for (JsonElement jsonElement : results) {
			JsonObject jo = jsonElement.getAsJsonObject();
			OperationProductLimit op = mop.get(jo.get("ProductId").getAsString());
			// 异常case的情况 没找到默认意味着空数据/不存在数据
			if (op == null) {
				Logger.verifyEquals(0, jo.get("ActivityLimitNumber").getAsInt(), "验证ActivityLimitNumber");
				Logger.verifyEquals(0, jo.get("ActivityBoughtNumber").getAsInt(), "验证ActivityBoughtNumber");
				Logger.verifyEquals(0, jo.get("ProductInActivityLimitNumber").getAsInt(), "验证ProductInActivityLimitNumber");
				Logger.verifyEquals(0, jo.get("ProductInActivityBoughtNumber").getAsInt(), "验证ProductInActivityBoughtNumber");
				continue;
			}
			if (op.getActivityLimit() != null && op.getActivityLimit() > 0) {
				Logger.verifyEquals(op.getActivityLimit(), jo.get("ActivityLimitNumber").getAsInt(), "验证ActivityLimitNumber");
				Logger.verifyEquals(op.getBuyCount(), jo.get("ActivityBoughtNumber").getAsInt(), "验证ActivityBoughtNumber");
			} else if (op.getProductInActivityLimit() != null && op.getProductInActivityLimit() > 0) {
				Logger.verifyEquals(op.getProductInActivityLimit(), jo.get("ProductInActivityLimitNumber").getAsInt(), "验证ProductInActivityLimitNumber");
				Logger.verifyEquals(op.getBuyCount(), jo.get("ProductInActivityBoughtNumber").getAsInt(), "验证ProductInActivityBoughtNumber");
			}
		}

	}

	private void verifyDataNull(GetLimitAllowBuyCall getlimitallowbuyCall2,int ActivityLimitNumber,int ProductInActivityLimitNumber) {
		JsonArray results = getlimitallowbuyCall2.getJsonObject().getAsJsonObject("Data").getAsJsonArray("Results");
		for (JsonElement jsonElement : results) {
			JsonObject jo = jsonElement.getAsJsonObject();
			// 异常case的情况 没找到默认意味着空数据/不存在数据
			Logger.verifyEquals(ActivityLimitNumber, jo.get("ActivityLimitNumber").getAsInt(), "验证ActivityLimitNumber");
			Logger.verifyEquals(0, jo.get("ActivityBoughtNumber").getAsInt(), "验证ActivityBoughtNumber");
			Logger.verifyEquals(ProductInActivityLimitNumber, jo.get("ProductInActivityLimitNumber").getAsInt(), "验证ProductInActivityLimitNumber");
			Logger.verifyEquals(0, jo.get("ProductInActivityBoughtNumber").getAsInt(), "验证ProductInActivityBoughtNumber");
		}

	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetLimitAllowBuy_001() {
		Logger.start(true, "获取活动限购信息成功ActivityLimit");
		try {
			// 注册用户
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();

			// 操作,写入限购数据
			OperationProductLimitBean operationproductlimitBean = ProductLimitCallService.initOperationProductLimitBean(userId, 1, 0, 1, 1);
			ProductLimitCallService.operationProductLimitCall(operationproductlimitBean);

			getlimitallowbuyBean = ProductLimitCallService.initGetlimitallowbuyBean(operationproductlimitBean);
			getlimitallowbuyCall.setData(getlimitallowbuyBean);
			getlimitallowbuyCall.callService();
			Logger.verifyEquals("200", getlimitallowbuyCall.getString("Code"), "验证Code");

			Logger.verifyEquals("200", getlimitallowbuyCall.getString("Code"), "验证Code");

			verifyData(getlimitallowbuyCall, operationproductlimitBean);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_GetLimitAllowBuy_002() {
		Logger.start(true, "批量获取活动限购信息成功");
		try {
			// 注册用户
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();

			// 操作,写入限购数据
			OperationProductLimitBean operationproductlimitBean = ProductLimitCallService.initOperationProductLimitBean(userId, 1, 0, 1, 2);
			ProductLimitCallService.operationProductLimitCall(operationproductlimitBean);

			getlimitallowbuyBean = ProductLimitCallService.initGetlimitallowbuyBean(operationproductlimitBean);
			getlimitallowbuyCall.setData(getlimitallowbuyBean);
			getlimitallowbuyCall.callService();

			Logger.verifyEquals("200", getlimitallowbuyCall.getString("Code"), "验证Code");

			verifyData(getlimitallowbuyCall, operationproductlimitBean);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetLimitAllowBuy_003() {
		Logger.start(true, "输入不存在用户id获取不存在限购信息为空");
		try {
			// 注册用户
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();

			// 操作,写入限购数据
			OperationProductLimitBean operationproductlimitBean = ProductLimitCallService.initOperationProductLimitBean(userId, 1, 0, 1, 1);
			ProductLimitCallService.operationProductLimitCall(operationproductlimitBean);

			getlimitallowbuyBean = ProductLimitCallService.initGetlimitallowbuyBean(operationproductlimitBean);
			getlimitallowbuyBean.setBuyerId(-1);
			getlimitallowbuyCall.setData(getlimitallowbuyBean);
			getlimitallowbuyCall.callService();
			Logger.verifyEquals("201", getlimitallowbuyCall.getString("Code"), "验证Code");
			Logger.verifyEquals("false", getlimitallowbuyCall.getString("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetLimitAllowBuy_004() {
		Logger.start(true, "输入不存在活动id获取不存在限购信息为空");
		try {
			// 注册用户
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();

			// 操作,写入限购数据
			OperationProductLimitBean operationproductlimitBean = ProductLimitCallService.initOperationProductLimitBean(userId, 1, 0, 1, 1);
			ProductLimitCallService.operationProductLimitCall(operationproductlimitBean);

			getlimitallowbuyBean = ProductLimitCallService.initGetlimitallowbuyBean(operationproductlimitBean);
			getlimitallowbuyBean.getCatalogList().get(0).setProductInActivityId(null);
			getlimitallowbuyBean.getCatalogList().get(0).setActivityId(-1);
			getlimitallowbuyCall.setData(getlimitallowbuyBean);
			getlimitallowbuyCall.callService();
			Logger.verifyEquals("200", getlimitallowbuyCall.getString("Code"), "验证Code");

			verifyDataNull(getlimitallowbuyCall,0,0);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetLimitAllowBuy_005() {
		Logger.start(true, "输入不存在商品id获取不存在限购信息为空");
		try {
			// 注册用户
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();

			// 操作,写入限购数据
			OperationProductLimitBean operationproductlimitBean = ProductLimitCallService.initOperationProductLimitBean(userId, 1, 0, 1, 1);
			ProductLimitCallService.operationProductLimitCall(operationproductlimitBean);

			getlimitallowbuyBean = ProductLimitCallService.initGetlimitallowbuyBean(operationproductlimitBean);
			getlimitallowbuyBean.getCatalogList().get(0).setProductId(null);
			getlimitallowbuyCall.setData(getlimitallowbuyBean);
			getlimitallowbuyCall.callService();
			Logger.verifyEquals("201", getlimitallowbuyCall.getString("Code"), "验证Code");
			Logger.verifyEquals("false", getlimitallowbuyCall.getString("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetLimitAllowBuy_006() {
		Logger.start(true, "输入不存在活动商品id_ProductInActivityId,会按aid和pid获取");
		try {
			// 注册用户
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();

			// 操作,写入限购数据
			OperationProductLimitBean operationproductlimitBean = ProductLimitCallService.initOperationProductLimitBean(userId, 1, 0, 1, 1);
			ProductLimitCallService.operationProductLimitCall(operationproductlimitBean);

			getlimitallowbuyBean = ProductLimitCallService.initGetlimitallowbuyBean(operationproductlimitBean);
			getlimitallowbuyBean.getCatalogList().get(0).setProductInActivityId(null);
			getlimitallowbuyCall.setData(getlimitallowbuyBean);
			getlimitallowbuyCall.callService();
			Logger.verifyEquals("200", getlimitallowbuyCall.getString("Code"), "验证Code");
			verifyData(getlimitallowbuyCall, operationproductlimitBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetLimitAllowBuy_007() {
		Logger.start(true, "批量获取限购信息，其中有一部分信息不存在,返回正确数据");
		try {
			// 注册用户
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();

			// 操作,写入限购数据
			OperationProductLimitBean operationproductlimitBean = ProductLimitCallService.initOperationProductLimitBean(userId, 0, 1, 1, 2);
			ProductLimitCallService.operationProductLimitCall(operationproductlimitBean);

			getlimitallowbuyBean = ProductLimitCallService.initGetlimitallowbuyBean(operationproductlimitBean);
			getlimitallowbuyBean.getCatalogList().get(0).setProductId(TestDataManager.getRandomUUID().toString());
			getlimitallowbuyCall.setData(getlimitallowbuyBean);
			getlimitallowbuyCall.callService();
			Logger.verifyEquals("200", getlimitallowbuyCall.getString("Code"), "验证Code");

			Logger.verifyEquals("200", getlimitallowbuyCall.getString("Code"), "验证Code");

			verifyData(getlimitallowbuyCall, operationproductlimitBean);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetLimitAllowBuy_008() {
		Logger.start(true, "获取活动商品限购信息成功ProductInActivityLimit");
		try {
			// 注册用户
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();

			// 操作,写入限购数据
			OperationProductLimitBean operationproductlimitBean = ProductLimitCallService.initOperationProductLimitBean(userId, 0, 1, 1, 1);
			ProductLimitCallService.operationProductLimitCall(operationproductlimitBean);

			getlimitallowbuyBean = ProductLimitCallService.initGetlimitallowbuyBean(operationproductlimitBean);
			getlimitallowbuyCall.setData(getlimitallowbuyBean);
			getlimitallowbuyCall.callService();
			Logger.verifyEquals("200", getlimitallowbuyCall.getString("Code"), "验证Code");

			Logger.verifyEquals("200", getlimitallowbuyCall.getString("Code"), "验证Code");

			verifyData(getlimitallowbuyCall, operationproductlimitBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	@Ignore
	//数据需要手工准备
	public void Tc_GetLimitAllowBuy_009() {
		Logger.start(true, "一个商品同时出现在隔离和非隔离2个活动中,查询数据");
		try {
			getlimitallowbuyBean.setAppId("AutoTest");
			getlimitallowbuyBean.setBuyerId(20336378);
			List<CatalogLimit> catalogList=new ArrayList<>();
				CatalogLimit cataloglimit=new CatalogLimit();
				cataloglimit.setActivityId(24059);
				cataloglimit.setProductId("bcde86db-1bf2-4403-b8bb-ccdfcffe4703");
				catalogList.add(cataloglimit);
				
//				CatalogLimit cataloglimit2=new CatalogLimit();
//				cataloglimit2.setActivityId(24062);
//				cataloglimit2.setProductId("bcde86db-1bf2-4403-b8bb-ccdfcffe4703");
//				catalogList.add(cataloglimit2);
				
			getlimitallowbuyBean.setCatalogList(catalogList);
			getlimitallowbuyCall.setData(getlimitallowbuyBean);
			getlimitallowbuyCall.callService();
			Logger.verifyEquals("200", getlimitallowbuyCall.getString("Code"), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}